package com.woyowodaows.activity.home;

import android.os.Message;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.woyowodaows.R;
import com.woyowodaows.activity.mine.AddressManageActivity;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.bean.home.NewNoticeBean;
import com.woyowodaows.bean.home.NewNoticeData;
import com.woyowodaows.bean.home.NoticeBean;
import com.woyowodaows.bean.mine.NewBean;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.ToastUtil;
import com.woyowodaows.weight.LoadingDataLayout;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/3/28.
 * 新闻详情
 */

public class NewDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_tilte_conets)
    TextView tvTilteConets;
    @BindView(R.id.tv_tilte_times)
    TextView tvTilteTimes;
    @BindView(R.id.tv_order_state)
    TextView tvOrderState;
    @BindView(R.id.wv_news_detail_body)
    WebView wbUrl;

    private NewBean bean;

     private NewNoticeBean note;
    @Override
    public int getLayoutId() {
        return R.layout.activity_new_noticedetails;
    }

    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    setHtmlData(note.getContent());
                    break;

            }
        }
    };

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitleBar(false, "消息详情", "", 0, 0, null);
        if(getIntent()!=null){
            bean = (NewBean) this.getIntent().getSerializableExtra("news");
        }
        tvTilteConets.setText(bean.getTitle());
        if(bean.getCreateTime()==null){
            SimpleDateFormat formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日   HH:mm:ss");
            Date curDate =  new Date(System.currentTimeMillis());
            String   str   =   formatter.format(curDate);
            tvTilteTimes.setText(str);
        }else {
            tvTilteTimes.setText(bean.getCreateTime());
        }

        if(bean.getType().equals("notice")){
            tvOrderState.setBackgroundResource(R.drawable.shape_bg_red_small);
            tvOrderState.setText("公告");
        }else {//提醒
            tvOrderState.setText("提醒");
            tvOrderState.setBackgroundResource(R.drawable.shape_button_green);
        }
        wbUrl.setEnabled(false);
        getData();

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private  void getData(){
            Map<String, Object> mapParameter = new HashMap<>();
            mapParameter.put("id",bean.getId());
            HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.MESSAGEDETAIL, NewNoticeData.class, mapParameter, new HttpRequestListener() {
                @Override
                public void onSuccess(Object result) {
                    if (result != null) {
                        NewNoticeData bean = (NewNoticeData) result;
                        if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                             if(bean.getData()!=null){
                                 note=bean.getData();
                                 if(note.getContent()!=null){
                                   handler.sendEmptyMessage(0);
                                 }

                             }
                        }
                    }
                }

                @Override
                public void onError(String message) {
                }
            });

    }


    public void setHtmlData(String content) {
        WebSettings webSettings = wbUrl.getSettings();
        //设置webview是否支持缩放
        webSettings.setSupportZoom(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setDefaultTextEncodingName("utf-8"); //设置文本编码
        webSettings.setAppCacheEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);//设置缓存模式
        String html = content.replace("<img", "<img style=\"display:        ;max-width:100%;\"");
        wbUrl.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
        wbUrl.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            /**
             *   webView 成功加载后数据加载
             * @param view
             * @param url
             */
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

        });
    }

}
